﻿Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Drawing.Design
Imports System.Windows.Forms

Namespace System.Workflow.ComponentModel.Design
    Friend NotInheritable Class ColorPickerEditor
        Inherits UITypeEditor
        ' Methods
        Public Overrides Function EditValue(ByVal context As ITypeDescriptorContext, ByVal provider As IServiceProvider, ByVal value As Object) As Object
            Dim dialog As New ColorDialog { _
                .AllowFullOpen = True, _
                .FullOpen = True, _
                .AnyColor = True, _
                .Color = If(TypeOf value Is Color, DirectCast(value, Color), Color.White) _
            }
            If (dialog.ShowDialog = DialogResult.OK) Then
                Return dialog.Color
            End If
            Return value
        End Function

        Public Overrides Function GetEditStyle(ByVal context As ITypeDescriptorContext) As UITypeEditorEditStyle
            Return UITypeEditorEditStyle.Modal
        End Function

        Public Overrides Function GetPaintValueSupported(ByVal context As ITypeDescriptorContext) As Boolean
            Return ((Not context Is Nothing) AndAlso (Not context.PropertyDescriptor Is Nothing))
        End Function

        Public Overrides Sub PaintValue(ByVal e As PaintValueEventArgs)
            MyBase.PaintValue(e)
            If TypeOf e.Value Is Color Then
                Dim color As Color = DirectCast(e.Value, Color)
                If (color <> Color.Empty) Then
                    [Using] brush As Brush = New SolidBrush(color)
                        e.Graphics.FillRectangle(brush, e.Bounds)
                    End [Using]
                End If
            End If
        End Sub

    End Class
End Namespace

